package com.example.black.myapplication.mynet;

import android.support.annotation.NonNull;
import android.util.Log;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class HttpTask<T> implements Runnable, Delayed {
    public static final int MAX_COUNT = 3;
    public static final int DELAYTIME = 3000;
    private IRequest request;
    private long delayTime = 3000;
    public int count;
    private static final String TAG = Request.class.getSimpleName();
    public HttpTask(String url,byte[] data, IJSONResponseCallBack<T> callBack,Class<T> responseClass) {
        Log.e(TAG, "HttpTask: ");
        this.request = new Request(url,data,new JsonRequest<>(responseClass,callBack));
    }

    @Override
    public void run() {
        try {
            request.execute();
        }catch (Exception e){
            e.printStackTrace();
            NetWorkManager.getNetWorkManager().addDelayTask(this);
        }
    }

    public void setDelayTime(long delayTime) {
        this.delayTime = System.currentTimeMillis()+delayTime;
    }

    @Override
    public long getDelay(@NonNull TimeUnit unit) {
        return unit.convert( this.delayTime-System.currentTimeMillis(),TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(@NonNull Delayed o) {
        return 0;
    }


}
